package zcjq.d907;

import java.util.Scanner;

public class P2 {

    public int f(int n) {
        String s = String.valueOf(n);
        int[] nums = new int[s.length()];
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            nums[i] = chars[i] - '0';
        }
        //处理
        for (int i = 0; i < nums.length; i++) {
            // 合法
            if (1 <= nums[i] && nums[i] <= 3) {
                continue;
            } else if (nums[i] > 3) { //不合法
                while (i < nums.length) {
                    nums[i] = 3;
                    i++;
                }
                break;
            } else { //当前位为0
                //借位
                solveZero(nums, i);
                i++;
                while (i < nums.length) {
                    nums[i] = 3;
                    i++;
                }
                break;
            }
        }
        int total = 0;
        for (int num : nums) {
            total = total * 10 + num;
        }
        return total;
    }

    private void solveZero(int[] nums, int idx) {
        if (idx <= 0) {
            return;
        } else {
            if (idx - 1 >= 0) {//向前一位借位
                nums[idx - 1] -= 1;
                if (nums[idx - 1] == 0) {
                    solveZero(nums, idx - 1);
                }
                nums[idx] = 3;
            }
        }
    }

    public static void main(String[] args) {
        P2 p2 = new P2();
        Scanner scanner = new Scanner(System.in);
        int T = Integer.parseInt(scanner.nextLine());
        while (T-- > 0) {
            int n = Integer.parseInt(scanner.nextLine());
            System.out.println(p2.f(n));
        }
    }
}
